from typing import List, Optional
from pydantic import BaseSettings, Field
import os


class Settings(BaseSettings):
    # Server Configuration
    host: str = Field(default="0.0.0.0", env="HOST")
    port: int = Field(default=8000, env="PORT")
    debug: bool = Field(default=False, env="DEBUG")
    
    # Database
    database_url: str = Field(env="DATABASE_URL")
    redis_url: str = Field(env="REDIS_URL")
    
    # Storage
    storage_type: str = Field(default="local", env="STORAGE_TYPE")
    s3_bucket_name: str = Field(env="S3_BUCKET_NAME")
    aws_access_key_id: str = Field(env="AWS_ACCESS_KEY_ID")
    aws_secret_access_key: str = Field(env="AWS_SECRET_ACCESS_KEY")
    aws_region: str = Field(default="us-east-1", env="AWS_REGION")
    minio_endpoint: str = Field(env="MINIO_ENDPOINT")
    minio_access_key: str = Field(env="MINIO_ACCESS_KEY")
    minio_secret_key: str = Field(env="MINIO_SECRET_KEY")
    
    # AI Model Configuration
    model_path: str = Field(default="./models", env="MODEL_PATH")
    model_name: str = Field(
        default="runwayml/stable-diffusion-v1-5", 
        env="MODEL_NAME"
    )
    controlnet_model: str = Field(
        default="lllyasviel/sd-controlnet-canny",
        env="CONTROLNET_MODEL"
    )
    device: str = Field(default="auto", env="DEVICE")
    torch_compile: bool = Field(default=False, env="TORCH_COMPILE")
    compile_mode: str = Field(default="reduce-overhead", env="COMPILE_MODE")
    
    # Security
    secret_key: str = Field(env="SECRET_KEY")
    algorithm: str = Field(default="HS256", env="ALGORITHM")
    access_token_expire_minutes: int = Field(default=30, env="ACCESS_TOKEN_EXPIRE_MINUTES")
    max_file_size: int = Field(default=10 * 1024 * 1024, env="MAX_FILE_SIZE")  # 10MB
    allowed_extensions: List[str] = Field(
        default=["png", "jpg", "jpeg", "webp"],
        env="ALLOWED_EXTENSIONS"
    )
    
    # Monitoring
    sentry_dsn: Optional[str] = Field(default=None, env="SENTRY_DSN")
    prometheus_port: int = Field(default=8001, env="PROMETHEUS_PORT")
    
    # Celery
    celery_broker_url: str = Field(env="CELERY_BROKER_URL")
    celery_result_backend: str = Field(env="CELERY_RESULT_BACKEND")
    
    class Config:
        env_file = ".env"
        case_sensitive = False


# Create settings instance
settings = Settings()
